package com.fingard.dsp.bank.directbank.bea01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BEA01SinglePay extends BEA01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        tmpRetDetail = d_190802(p_req1908);
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    //190802对外支付
    private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BEA01Header bankHeader = new BEA01Header();
        bankHeader.SERVICE_CODE = "3101";
        bankHeader.TRAN_REF_NO = p_req1908.singleDetail.askNumber;

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        XElement tmpXElement = new XElement("bcdl", "");
        tmpXElement.addChild(new XElement("SERVICE_CODE", bankHeader.SERVICE_CODE));//交易代码
        tmpXElement.addChild(new XElement("TRAN_REF_NO", bankHeader.TRAN_REF_NO));//指令序列号
        tmpXElement.addChild(new XElement("TRAN_FLAG", "1".equals(p_req1908.singleDetail.sameBnk) ? "00" : "01"));//转账标识 00:行内转账  01:行外转账
        tmpXElement.addChild(new XElement("ACCT_DEBIT_NO", p_req1908.singleDetail.payAct));//付款方账号
        tmpXElement.addChild(new XElement("ACCT_DEBIT_NAME", p_req1908.singleDetail.payName));//付款账号户名
        tmpXElement.addChild(new XElement("ACCT_CREDIT_NO", p_req1908.singleDetail.recAct));//收款方帐号
        tmpXElement.addChild(new XElement("ACCT_CREDIT_NAME", p_req1908.singleDetail.recName));//收款方户名
        tmpXElement.addChild(new XElement("BANK_CODE", p_req1908.singleDetail.cnapsCode));//联行行号
        tmpXElement.addChild(new XElement("CCY", convertCurToBank(p_req1908.singleDetail.payCur)));//币种
        tmpXElement.addChild(new XElement("TRAN_AMT", StringHelper.amountMultiply100(p_req1908.singleDetail.payAmount)));//交易金额 单位:分
        tmpXElement.addChild(new XElement("TRAN_REMARK", p_req1908.singleDetail.purpose));//用途
        tmpXElement.addChild(new XElement("ACCT_CREDIT_TYPE", "1".equals(p_req1908.singleDetail.isPrivate) ? "1" : "0"));//收款方类型 0:企业    1:个人
        tmpXElement.addChild(new XElement("PRIORITY", "1".equals(p_req1908.singleDetail.paySpd) ? "1" : "0"));//加急标志 0:普通    1:加急 转账金额大于5W走大额，加急
        tmpXElement.addChild(new XElement("reqTime", ""));
        tmpXElement.addChild(new XElement("CHECKPS", ""));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String retCode = tmpBkReader.getFirstText("/BCDL/opRep/retCode");
                String errMsg = tmpBkReader.getFirstText("/BCDL/opRep/errMsg");
                if ("000000".equals(retCode)) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpBkReader.getFirstText("/BCDL/opRep/opResult/TRAN_MSG");
                    retDetail.transState = "000000+" + tmpBkReader.getFirstText("/BCDL/opRep/opResult/TRAN_FLAG");
                    retDetail.payInfoCode = "000000+" + tmpBkReader.getFirstText("/BCDL/opRep/opResult/TRAN_FLAG");
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                } else {
                    retDetail.respCode = "0";
                    retDetail.respInfo = errMsg;
                    retDetail.transState = retCode;
                    retDetail.payInfoCode = retCode;
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);

        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BEA01Header bankHeader = new BEA01Header();
        bankHeader.SERVICE_CODE = "3301";
        bankHeader.TRAN_REF_NO = getQryTransID();
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, bankHeader);//写入通用的头部节点
        xmlWriter.writeElementString("TRAN_REF_NO_1", p_req1944.askNumber);
        xmlWriter.writeElementString("reqTime", "", true);
        xmlWriter.writeElementString("CHECKPS", "", true);
        xmlWriter.writeEndDocument();

        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String retCode = tmpBkReader.getFirstText("/BCDL/opRep/retCode");
                    String errMsg = tmpBkReader.getFirstText("/BCDL/opRep/errMsg");
                    if ("000000".equals(retCode)) {
                        retDetail.respCode = "0";
                        retDetail.transState = "000000+" + tmpBkReader.getFirstText("/BCDL/opRep/opResult/TRAN_FALG");
                        retDetail.reqSeqID = p_req1944.askNumber;
                        retDetail.respInfo = tmpBkReader.getFirstText("/BCDL/opRep/opResult/RET_MSG_1");
                        retDetail.payInfoCode = retDetail.transState;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = errMsg;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
